﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.BasicSettings
{
    //待宰圈管理
    public class SlaughteredSuretenHandler : IRequestHandler<SlaughteredSuretenCommend, Result>
    {
        private readonly IBaseRepositoryGet<Slaughteredtes> Slaughteredtes;
        private readonly IBaseRepositoryGet<LiveAnimals> LiveAnimals;

        private readonly IBaseRepositoryGet<UPCData> UPCData;
        private readonly IBaseRepositoryGet<Waitings> Waitings;
        private readonly IBaseRepositoryGet<CheckIn> CheckIn;

        public SlaughteredSuretenHandler(IBaseRepositoryGet<Slaughteredtes> Slaughteredtes, IBaseRepositoryGet<LiveAnimals> LiveAnimals
            , IBaseRepositoryGet<CheckIn> CheckIn, IBaseRepositoryGet<UPCData> UPCData, IBaseRepositoryGet<Waitings> Waitings)
        {
            this.Slaughteredtes = Slaughteredtes;
            this.LiveAnimals = LiveAnimals;
            this.CheckIn = CheckIn;
            this.UPCData = UPCData;
            this.Waitings = Waitings;
        }

        public async Task<Result> Handle(SlaughteredSuretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in Slaughteredtes.GetAll()
                            join b in UPCData.GetAll() on a.MarkId equals b.GoodsMinuteId
                            join c in Waitings.GetAll() on a.WaitingId equals c.WaitingId
                            join d in LiveAnimals.GetAll() on a.LiveAnimalsId equals d.LiveAnimalsId
                            join e in CheckIn.GetAll() on d.CheckInId equals e.CheckInId
                            where a.IsDelete == false
                            select new

                            {
                                a.SlaughteredId,
                                a.SlaughteredTime,
                                a.SlaughteredRemark,
                                b.GoodsMinuteId,
                                b.UPCDataName,
                                c.WaitingId,
                                c.WaitingName,
                                d.LiveAnimalsId,
                                d.LiveAnimalsWeight,
                                d.LiveAnimalsRAverageWeight,
                                e.CheckInId,
                                e.CheckInNo,
                                e.ApproachNum,

                            }).ToList();


                if (request.WaitingId > 0)
                {
                    list = list.Where(x => x.WaitingId == request.WaitingId).ToList();
                }

                var totalcount = list.Count();
                var pagecount = (int)Math.Ceiling((totalcount * 1.0) / request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                var lief = new
                {
                    totalcount,
                    pagecount,
                    list,
                };

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = lief;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

    }
}
